package BuildTrainSet.AssignTag;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author phamthanh
 */
public class Group {

    public Group(String inF, String ouF) throws XMLStreamException, IOException{
        long startTime = System.currentTimeMillis();

        FileInputStream fis = new FileInputStream(inF);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        FileOutputStream fos = new FileOutputStream(ouF);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);

        System.out.println("package BuildTrainSet.AssignTag; class Group");

        xmlStreamWriter.writeStartElement("root");
        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {

                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("record")) {
                        String RecId = xmlStreamReader.getAttributeValue(0);
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeAttribute("RecId", RecId);

                    }

                    if (localName.equals("URL")) {
                        String url = xmlStreamReader.getElementText();

                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(url);
                        xmlStreamWriter.writeEndElement();

                    }
                    if (localName.equals("Infomation")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }
                    if (localName.equals("W")) {
                        String attributeName = xmlStreamReader.getAttributeLocalName(0);
                        String attributeValue = xmlStreamReader.getAttributeValue(0);
                        if (xmlStreamReader.getAttributeCount() == 1) {
                                String nsw = xmlStreamReader.getElementText();
                                xmlStreamWriter.writeStartElement(localName);
                                xmlStreamWriter.writeAttribute(attributeName, attributeValue);
                                xmlStreamWriter.writeCharacters(nsw);
                                xmlStreamWriter.writeEndElement();
                           
                        } else {
                            String nsw = xmlStreamReader.getElementText();                           
                            String classNsw = detectGroup(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", classNsw);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }
                    }

                    if (localName.equals("SPLIT")) {                       
                        xmlStreamWriter.writeStartElement(localName);                       
                    }
                    break;

                case XMLStreamConstants.END_ELEMENT:
                    String endLocalName = xmlStreamReader.getLocalName();

                    if (endLocalName.equals("record")) {
                        xmlStreamWriter.writeEndElement();
                    }
                    if (endLocalName.equals("Infomation")) {
                        xmlStreamWriter.writeEndElement();
                    }
                    
                    if(endLocalName.equals("SPLIT")){
                        xmlStreamWriter.writeEndElement();
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();
                    xmlStreamWriter.writeCharacters(character);
                    break;

                default:
                    break;
            }
        }

        xmlStreamWriter.writeEndElement();

        xmlStreamReader.close();
        xmlStreamWriter.close();
        fis.close();
        fos.close();
        long endTime = System.currentTimeMillis();
        long time=endTime-startTime;
        System.out.println("Thoi gian gan the num la: "+time);
    }

    public String detectGroup(String nsw) {
      String group="OTHER";
      Pattern NUMBER=Pattern.compile("\\d");
      Pattern ALPHA=Pattern.compile("\\p{L}");

      Matcher mn=NUMBER.matcher(nsw);
      Matcher ma=ALPHA.matcher(nsw);
      if(ma.find()){
          return "ALPHA";
      }
      if(mn.find()){
          return "NUMBER";

      }
        return group;
    }
}
